
xmlPath = "C:\Logs\XML-XSD\pain.008_Recu.xml"
xsdPath = "C:\Logs\XML-XSD\pain.008.001.01.xsd"
xmlTestFile = "C:\Logs\XML-XSD\Result.xml"

Set xmlDoc = LoadPainFile(xmlPath)
Call ValidatePain(xmlDoc,xsdPath)



'********************************************************
'This Procedure saves the xml file with required changes to
'a specified location. xmldocument Object and path are required
'********************************************************

Sub SaveXMLFile (ObjFile,UNCTestFilePath)
ObjFile.save(UNCTestFilePath)
End Sub

'********************************************************
'This Procedure sets the value of a Tag against the specified
'TagName and Value. Requires xmlDoc Object to be passed.
'This sub works for only Single Type PAIN File.
'********************************************************

Sub SetSingleValue (ObjxDoc, strTagName, strValue)
	Set strElement = ObjxDoc.selectsingleNode("//" & strTagName)
	strElement.text = strValue
End Sub


'********************************************************
'This function loads the XML file without validating it.
'********************************************************

Function LoadPainFile(xmlPath)
Dim ObjXMLDoc 
'Create XML DOMDocument Instance and set Options
	Set ObjXMLDoc = CreateObject("MSXML2.DOMDocument.4.0")
		ObjXMLDoc.async = False
		ObjXMLDoc.validateOnParse = False	
		ObjXMLDoc.resolveExternals = False	 
'Load XML without any validation
	If Not ObjXMLDoc.load(xmlPath) Then
		Set ObjParseErr = ObjXMLDoc.parseError
		MsgBox ObjParseErr.reason & ObjParseErr.srcText
		Err.Raise 1, "validate", "Laod XML Failed"
	End If
Set LoadPainFile = ObjXMLDoc
End Function


'**********************************************************
'This Function validates a XML against any specified XSD
'**********************************************************
Function ValidatePain(ObjXMLDoc,xsdPath)
Dim ObjXMLSchema
Dim ObjErr
'Create Schema Cache
	Set ObjXMLSchema = CreateObject("MSXML2.XMLSchemaCache.4.0")
'Populate the Schema Cache
	ObjXMLSchema.add "", xsdPath
'Bind XML to Schema
	ObjXMLDoc.schemas = ObjXMLSchema
'Check if XML is Loaded
	If ObjXMLDoc.readystate = 4 Then
		Set ObjErr = ObjXMLDoc.validate()
	Else 
	Err.Raise 1, "XML Loading", "XML not pre-loaded for validation"
	End If
'Prompt Error if any
	If ObjErr.errorcode <> 0 Then
	Err.Raise 1, "Validate", ObjErr.reason
	End If
	Set ValidatePain = ObjXMLDoc
End Function


'*********************************************************************
'This Function validates the XML file while loading only.
'The XML will only be validated if the location of DTD or XSD has 
'has been specified within the XML and if that location is accessible
'by this code (For Best Results place the XSD and XML in the same Dir)
'*********************************************************************

Function LoadAndValidateFile(xmlPath)
Dim ObjXMLDoc 
'Create XML DOMDocument Instance and Set Validation Options
	Set ObjXMLDoc = CreateObject("MSXML2.DOMDocument.4.0")
		ObjXMLDoc.async = False
		ObjXMLDoc.validateOnParse = True	
		ObjXMLDoc.resolveExternals = True
'Load XML and Validate
	If Not ObjXMLDoc.load(xmlPath) Then
		Set ObjParseErr = ObjXMLDoc.parseError
		Err.Raise 1, "Load and Validate", ObjParseErr.reason & ObjParseErr.srcText
	End If
Set LoadAndValidateFile = ObjXMLDoc
End Function


'**********************************************************************